/**
 * 
 */
package data;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import domain.Article;
import domain.LocalPrinter;
import domain.NetworkPrinter;
import domain.Printer;
import domain.Product;
import domain.Server;
import domain.Supplier;

/**
 * @author grupo22
 *
 */
public class JDBCPrinterDAO implements IPrinterDAO {

	/* (non-Javadoc)
	 * @see data.IPrinterDAO#select(java.sql.Connection, java.lang.String, domain.Printer)
	 */
	@Override
	public Printer select(Connection con, String oid, String brand, String model, double price, Article article, Supplier supplier) {
		String sql = "SELECT * FROM Printer WHERE (OIDProduct = ? )";
        PreparedStatement stmt = null;
        ResultSet rs = null;
        Printer p = null;

        try {
            stmt = con.prepareStatement(sql);
            stmt.setString(1, oid);

            rs = stmt.executeQuery();

            rs.next();

        	int ppm = rs.getInt("ppm");
            boolean duplex = rs.getBoolean("duplex");
            boolean nic = rs.getBoolean("nic");
        	
            if (rs.getString("objectType").compareTo("network")==0){
            	boolean snmpCapable = rs.getBoolean("snmpCapable");
            	boolean debianComp = rs.getBoolean("debianComp");
            	p = new NetworkPrinter(brand,model,price,article,supplier,ppm,duplex,nic,snmpCapable,debianComp);
            	
            } else if (rs.getString("objectType").compareTo("local")==0){
            	boolean usbCableInc = rs.getBoolean("usbCableInc");
            	boolean guadalinexComp = rs.getBoolean("guadalinexComp");
            	p = new LocalPrinter(brand,model,price,article,supplier,ppm,duplex,nic,usbCableInc,guadalinexComp);
            }
            
        } catch (SQLException e) {
            System.out.println("Message: " + e.getMessage());
            System.out.println("SQLState: " + e.getSQLState());
            System.out.println("ErrorCode: " + e.getErrorCode());
        } finally {
            try {
                if (rs != null) {
                    rs.close();
                }
                if (stmt != null) {
                    stmt.close();
                }
            } catch (SQLException e) { }
        }
        return p;
	}
	
	@Override
	public void insert(Connection con, LocalPrinter p, String oid) {
		String sql = "INSERT INTO Printer (OIDProduct, ppm, duplex, nic, objectType, usbCableInc, guadalinexComp) VALUES (?,?,?,?,?,?,?)";
        PreparedStatement stmt = null;

        try {
            stmt = con.prepareStatement(sql);
            stmt.setString(1, oid);
            stmt.setInt(2, p.getPpm());
            stmt.setBoolean(3, p.isDuplex());
            stmt.setBoolean(4, p.isNic());
            stmt.setString(5, "local");
            stmt.setBoolean(6, p.isUsbCableInc());
            stmt.setBoolean(7, p.isGuadalinexComp());

            stmt.executeUpdate();
            
        } catch (SQLException e) {
            System.out.println("Message: " + e.getMessage());
            System.out.println("SQLState: " + e.getSQLState());
            System.out.println("ErrorCode: " + e.getErrorCode());
        } finally {
            try {
                if (stmt != null) {
                    stmt.close();
                }
            } catch (SQLException e) { }
        }
	}
	
	@Override
	public void insert(Connection con, NetworkPrinter p, String oid) {
		String sql = "INSERT INTO Printer (OIDProduct, ppm, duplex, nic, objectType, snmpCapable, debianComp) VALUES (?,?,?,?,?,?,?)";
        PreparedStatement stmt = null;

        try {
            stmt = con.prepareStatement(sql);
            stmt.setString(1, oid);
            stmt.setInt(2, p.getPpm());
            stmt.setBoolean(3, p.isDuplex());
            stmt.setBoolean(4, p.isNic());
            stmt.setString(5, "local");
            stmt.setBoolean(6, p.isSnmpCapable());
            stmt.setBoolean(7, p.isDebianComp());

            stmt.executeUpdate();
            
        } catch (SQLException e) {
            System.out.println("Message: " + e.getMessage());
            System.out.println("SQLState: " + e.getSQLState());
            System.out.println("ErrorCode: " + e.getErrorCode());
        } finally {
            try {
                if (stmt != null) {
                    stmt.close();
                }
            } catch (SQLException e) { }
        }
	}
}
